package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.inv.InvSaleInfo;
import light.head.model.inv.InvSaleInfoItem;

import org.springframework.stereotype.Service;
/**
 * 零售信息
 * @author yy
 *
 */
@Service
public class InvSaleInfoDAO extends BaseDAO<InvSaleInfo, Integer>{
	/**
	 * 根据机构编号查询零售单信息
	 * @param oc 机构编号
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null || ocs.size() == 0) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvSaleInfo s WHERE s.oc IN (");
		List<Object> list =new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String sname = map.get(Parm.SNAME); //销售员
			String cashier = map.get(Parm.CASHIER);	//收银员
			String dname = map.get(Parm.DNAME);
			String dn = map.get(Parm.DN);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.deleted = ?");
			}
			if(isNotNull(time1)){
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND s.ct >= ?");
			}
			if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append(" AND s.ct <= ?");
			}
			if(isNotNull(sname)){
				list.add("%" + sname + "%");
				hql.append(" AND s.smc IN (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
			}
			if(isNotNull(cashier)){
				list.add("%" + cashier + "%");
				hql.append(" AND s.cac IN (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}
			if(isNotNull(dn)){
				list.add("%" + dn + "%");
				hql.append(" AND s.dn LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	/**
	 * 根据机构编号查询零售单信息
	 * @param oc 机构编号
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public List<InvSaleInfoItem> findByMap1(List<Integer> ocs, Map<String, String> map) {
		if(ocs == null || ocs.size() == 0) return null;
		StringBuffer temp = new StringBuffer("FROM InvSaleInfoItem si WHERE 1= 1");
		List<Object> list =new ArrayList<Object>();
		/*for (Integer i : ocs) {
			temp.append("?,");
			list.add(i);
		}
		temp.deleteCharAt(temp.length() - 1).append(")");*/
		
		if(map != null){
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			
			if(isNotNull(time1)){
				list.add(DateUtils.getDateByStr(time1, null));
				temp.append(" AND si.time >= ?");
			}
			if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				temp.append(" AND si.time <= ?");
			}
		
		}
		temp.append(" ORDER BY si.code DESC");
		return find(temp.toString(), list.toArray());
	}
}
